package middleware

import (
	"github.com/starmotioncf/common/log"
	"github.com/starmotioncf/soutchatinternal"
	"github.com/starmotioncf/soutchatinternal/constant"
	"github.com/starmotioncf/soutchatinternal/service"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// RestrictionMiddleware 访问受限和封禁情况
type RestrictionMiddleware struct {
	restrictionService service.RestrictionService
}

func NewRestrictionMiddleware(restrictionService service.RestrictionService) *RestrictionMiddleware {
	return &RestrictionMiddleware{
		restrictionService: restrictionService,
	}
}

func (r *RestrictionMiddleware) Handle(ctx *gin.Context) {
	rs := r.restrictionService.GetRestrictions(ctx)
	//检查是否直接被封禁了
	if _rs, ban := rs.Banned(); ban {
		log.WithContext(ctx).Error("用户被封禁", zap.String("restriction", internal.ToJson(_rs)))
		ctx.Next()
	}
	ctx.Set(constant.CtxRestriction, rs)
	ctx.Next()
}
